#include "Channels.h"
#include "ChannelManager.h"
#include "Resource.h"
#include "String.h"
#include "SharedPtr.h"
#include "ResourceManager.h"
#include "Database/DatabaseEnv.h"
#include "StringConverter.h"
#include "NGLog.h"
#include "Character.h"
#include "CharManager.h"
#include "Items.h"
#include "ItemManager.h"
#include "WorldPacket.h"
#include "Towns.h"
#include "Effects.h"
#include "EffectManager.h"
#include "EffectModels.h"
#include "EffectModelManager.h"
#include "Users.h"
#include "UserManager.h"
#include "XMLData.h"
#include "Towns.h"
#include "Message.h"
#include "XMLData.h"
#include "OpcodesEx.h"
#include "VipCardManager.h"
#include "Tools.h"
#include "WSSocket.h"

#include "twmjn/TWMJOpcode.h"
#include "twmjn/TWMJAIInterface.h"
#include "twmjn/TWMJGameStruct.h"
#include "twmjn/TWMJXmlDataManager.h"
#include "MJEnum.h"

#include "GameDefine.h"


#include LOGIC_MGR_PATH
#include PROTOCOLS_MGR_PATH
#include XMLDATA_MGR_PATH
#include LOGIC_MGR_PATH

using namespace AIScript;

// SMSG_ENTERROOM = 0x0011,			//加入房间
bool 	TWMJProtocolsManager::CreateJoinGamePacket(WorldPacket * packet, const uint8 & type)
{
	if (!packet)
		return false;
	packet->clear();
	packet->SetOpcode(SMSG_ENTERROOM);
	*packet << uint16(SMSG_ENTERROOM) << uint16(5)
		<< type;
	packet->SetLength(packet->size());
	return true;
}


//SMSS_TWMJ_chr = 0x0100,		//返回玩家信息
bool 	TWMJProtocolsManager::CreateCharInforPacket(WorldPacket * packet,CharPtr & chr)
{
	if(!packet || chr.isNull()) 
		return false;
	packet->clear();
	packet->SetOpcode(SMSS_TWMJ_chr);					//
	uint32 id = chr->getHandle();
	uint16 lv = chr->getUInt32Field("lv");
	uint32 exp = chr->getUInt32Field("exps");
	uint32 money = chr->getUInt32Field("speed");
	
	uint32 pid = 0;
	UserPtr pUser = sUserMgr.getByHandle(chr->getUInt32Field("userid"));
	if(!pUser.isNull())
	 pid = pUser->getUInt32Field("platform_id");
	
	*packet << uint16(SMSS_TWMJ_chr) << uint16(24)
		<< id
		<< money
		<< pid;
		
	AppendPacketString<uint8>(packet, chr->getCharName());
	AppendPacketString<uint8>(packet,chr->getStringField("desc"));
	
	sGLMgr.PacketPlayerSameInfo(packet, chr);
	
	packet->SetLength(packet->size());
	return true;
}
	
//SMSS_TWMJ_targetchr = 0x0101,	//返回目标玩家信息
bool 	TWMJProtocolsManager::CreateTargetCharInforPacket(WorldPacket * packet,CharPtr & chr)
{
	if(!packet || chr.isNull()) 
		return false;
	packet->clear();
	packet->SetOpcode(SMSS_TWMJ_targetchr);					
	uint32 id = chr->getHandle();
	uint32 shenglv = chr->getUInt32Field("ability");
	uint16 lv = chr->getUInt32Field("lv");
	uint32 money = chr->getUInt32Field("speed");
	uint32 jiluo = chr->getUInt32Field("defence_ext");
	uint32 allts = chr->getUInt32Field("motion");
	uint32 wints = chr->getUInt32Field("payment");
	
	uint32 pid = 0;
	UserPtr pUser = sUserMgr.getByHandle(chr->getUInt32Field("userid"));
	if(!pUser.isNull())
	{
		pid = pUser->getUInt32Field("platform_id");
	}
	
	
	uint16 xunzhang = chr->getUInt32Field("hp");
	*packet << uint16(SMSS_TWMJ_targetchr) << uint16(24)
		<< id
	//	<< uint8(lv)
		<< money
		<< pid;
		   
	AppendPacketString<uint8>(packet, chr->getCharName());
	AppendPacketString<uint8>(packet,chr->getStringField("desc"));
	
	sVipCardMgr.PacketVipInfo(packet,chr);
	sGLMgr.PacketPlayerSameInfo(packet, chr);
	
	packet->SetLength(packet->size());
	return true;
}

// SMSS_TWMJ_roomList = 0x0102,	//房间列表
bool 	TWMJProtocolsManager::CreateRoomListPacket(WorldPacket * packet, std::list<TownPtr> * town_list)
{
	if (!packet)
	{
		return false;
	}

	packet->clear();
	packet->SetOpcode(SMSS_TWMJ_roomList);
	*packet << uint16(SMSS_TWMJ_roomList) << uint16(6)
		<< uint8(town_list->size());
	std::list<TownPtr>::iterator iter = town_list->begin();
	for (; iter != town_list->end(); ++iter)
	{
		*packet << uint32((*iter)->getUInt32Field("town_id")); 			// 房间ID
			//<< uint32((*iter)->getUInt32Field("town_def")) 			// 底分
			//<< uint32((*iter)->getUInt32Field("town_att")); 			// 进入最低分
																		//	AppendPacketString<uint8>(packet,(*iter)->getStringField("name"));	
	}
	packet->SetLength(packet->size());
	return true;
}

//  SMSS_TWMJ_GoHall = 0x0103,	//进入大厅或者进入房间
bool 	TWMJProtocolsManager::CreateGoHallPacket(WorldPacket * packet, const uint8 Where)
{
	if (!packet)
		return false;
	packet->clear();
	packet->SetOpcode(SMSS_TWMJ_GoHall);
	*packet << uint16(SMSS_TWMJ_GoHall) << uint16(5)
		<< Where;
	packet->SetLength(packet->size());

	return true;
}

//CMSS_TWMJ_Ready = 0x0104,		//玩家准备
bool TWMJProtocolsManager::CreatePlayerReadyPacket(WorldPacket * packet, const uint8 & type)
{
	if (!packet)
		return false;
	packet->clear();
	packet->SetOpcode(CMSS_TWMJ_Ready);
	*packet << uint16(CMSS_TWMJ_Ready) << uint16(5)
		<< type;
	packet->SetLength(packet->size());
	return true;
}



// //SMSS_TWMJ_ChooseKouPaiNotify = 0x0105,	// 通知玩家扣牌
// bool TWMJProtocolsManager::CreateKouPaiNotifyPacket(WorldPacket * packet, const uint32 & ui32LeftTime)
// {
// 	if (!packet)
// 		return false;
// 	packet->clear();
// 	packet->SetOpcode(SMSS_TWMJ_ChooseKouPaiNotify);
// 	*packet << uint16(SMSS_TWMJ_ChooseKouPaiNotify) << uint16(5)
// 		<< uint8(ui32LeftTime);
// 	packet->SetLength(packet->size());
// 	return true;
// }

// //SMSS_TWMJ_PlayerChooseKouPai = 0x0106,	// 玩家选扣牌
// bool TWMJProtocolsManager::CreateChooseKouPaiPacket(WorldPacket * packet,const uint8 & ui8PlayerPosID, std::vector<uint8 > vecKouPaiList, const uint8 & result)
// {
// 	if (!packet)
// 		return false;
// 	packet->clear();
// 	packet->SetOpcode(SMSS_TWMJ_PlayerChooseKouPai);
// 	*packet << uint16(SMSS_TWMJ_PlayerChooseKouPai) << uint16(5)
// 		<< ui8PlayerPosID
// 		<< result;
// 	*packet << uint8(vecKouPaiList.size());
// 	/*for (uint8 i = 0; i < vecKouPaiList.size(); i++)
// 	{
// 		*packet << vecKouPaiList[i];
// 	}*/
// 	packet->SetLength(packet->size());
// 	return true;
// }


//		SMSS_MJ_GameOverCalc = 0x2519,		//结算
bool TWMJProtocolsManager::CreateJieSuanPacket(WorldPacket * packet, std::vector<MJBaseTablePlayer*>& vecPlayerList,std::vector<std::vector<uint8> >ui8HuaPaiList)
{
	if (!packet)
		return false;
	packet->clear();
	packet->SetOpcode(SMSS_MJ_GameOverCalc);
	*packet << uint16(SMSS_MJ_GameOverCalc) << uint16(5);
	*packet << uint8(vecPlayerList.size());//玩家数量

	for (uint8 i = 0; i < vecPlayerList.size(); i++)
	{
		TWMJTablePlayers * pTp = TO_TWMJTP(vecPlayerList[i]);
		
		*packet << uint8(pTp->pos);//玩家位置

		std::map<uint32, uint32> handCards = pTp->GetHandCards();
		std::map<uint32, uint32>::iterator iter;
		std::vector<uint8> vecHandCards;
		for (iter = handCards.begin(); iter != handCards.end(); ++iter)
		{
			for (uint8 k = 0; k < iter->second; k++)
			{
				vecHandCards.push_back(iter->first);
			}
		}
		
		*packet << uint8(vecHandCards.size());//手牌数量
		for (uint8 t = 0; t < vecHandCards.size(); t++)
		{
			*packet << uint8(vecHandCards[t]);//牌值
		}
		std::vector<tagWeaveItem> vecWeave = pTp->GetWeaveList();
		*packet << uint8(vecWeave.size());//吃碰杠数量
		for (uint8 t = 0; t < vecWeave.size(); t++)
		{
			*packet << uint8(vecWeave[t].ui8WeaveKind)//驰鹏杠类型
					<< uint8(vecWeave[t].ui8CenterCard)//中心牌值
					<< uint8(vecWeave[t].ui8Type);//杠牌类型
		}
		*packet << uint8(pTp->PlayerType) //胡牌类型
				<< uint8(pTp->AddTaiTypeList.size());//番型数量
		std::map<uint8, AddTaiType>::iterator fit = pTp->AddTaiTypeList.begin();
		for (fit; fit != pTp->AddTaiTypeList.end(); ++fit)
		{
			*packet << uint8(fit->second.Type)//番型
					<< uint8(fit->second.Mult)//倍数	
					<< uint8(fit->second.Num);//出现次数
			Log.Notice("TWMJProtocolsManager::CreateJieSuanPacket", "Type[%d] Mult[%d] Num[%d] ", uint8(fit->second.Type), uint8(fit->second.Mult), uint8(fit->second.Num));
		}
		*packet << uint32(ui8HuaPaiList[pTp->pos].size()) //附加数据(花牌数量)
				<< int32(pTp->winLurce) //输赢
				<< uint8(pTp->m_ui8ChiHuCard);//胡的牌
		Log.Debug("JieSuan", "pTp[%d]->winLurce:%d", pTp->pos, pTp->winLurce);
	}

	packet->SetLength(packet->size());
	return true;
}
//		SMSS_MJ_SubGameOverCale = 0x251C,		//小结算
bool TWMJProtocolsManager::CreateXiaoJieSuanPacket(WorldPacket * packet, std::vector<MJBaseTablePlayer*>& vecPlayerList)
{
	if (!packet)
		return false;
	packet->clear();
	packet->SetOpcode(SMSS_MJ_SubGameOverCale);
	*packet << uint16(SMSS_MJ_SubGameOverCale) << uint16(5);
	*packet << uint8(vecPlayerList.size());

	for (uint8 i = 0; i < vecPlayerList.size(); i++)
	{
		TWMJTablePlayers * pTp = TO_TWMJTP(vecPlayerList[i]);
		
		*packet << uint8(pTp->pos);

		*packet << uint8(pTp->m_ui8HuType)
			    << uint8(pTp->m_ui8HuPaiFanXing)
			    << uint32(pTp->m_ui32ExData);
		Log.Debug("JieSuan", "pTp[%d]->winLurce:%d", pTp->pos, pTp->winLurce);
		*packet << int32(pTp->winLurce);
		*packet << int32(pTp->score);
		*packet << pTp->m_ui8ChiHuCard;
	}

	packet->SetLength(packet->size());
	return true;
}
//抓牌顺序
bool TWMJProtocolsManager::CreateDrawOrderPacket(WorldPacket *packet, std::vector<uint8> &Alignment)
{
	if (!packet)
		return false;
	packet->clear();
	packet->SetOpcode(SMSS_MJ_DrawOrder);
	*packet << uint16(SMSS_MJ_DrawOrder) << uint16(5);
	*packet << uint8(Alignment.size());
	for (uint8 i = 0; i < Alignment.size(); ++i)
	{
		*packet << uint8(Alignment[i]);
		Log.Debug("TWMJProtocolsManager::CreateDrawOrderPacket", "Alignment[%d]:%d",i,Alignment[i]);
	}

		packet->SetLength(packet->size());

	return true;
}

	//发送骰子数
bool TWMJProtocolsManager::CreateBroadcastDiceResultPacket(WorldPacket *packet, std::vector<uint8> &m_dice)
{

	if (!packet)
		return false;
	packet->clear();
	packet->SetOpcode(SMSS_MJ_DiceResult);
	*packet << uint16(SMSS_MJ_DiceResult) << uint16(5);
	*packet << uint8(m_dice.size() - 1);
	for (uint8 i = 0; i < m_dice.size()-1; ++i)
	{
		*packet << uint8(m_dice[i]);
		Log.Debug("TWMJProtocolsManager::CreateBroadcastDiceResultPacket", "m_dice[%d]:%d", i, m_dice[i]);
	}

	packet->SetLength(packet->size());

	return true;
}

	//连庄次数
bool TWMJProtocolsManager::CreateBroadcastContinueBankerPacket(WorldPacket *packet, const TWMJTablePlayers *pTp)
{

	if (!packet)
		return false;
	if(pTp == NULL)
		return false;
	packet->clear();
	packet->SetOpcode(SMSS_MJ_ContinBank);
	*packet << uint16(SMSS_MJ_ContinBank) << uint16(5);
        *packet << uint8(pTp->banker_count);
        Log.Debug("TWMJProtocolsManager::CreateBroadcastContinueBankerPacket",
                  "banker_count:%d",pTp->banker_count);


	packet->SetLength(packet->size());

	return true;
}

//风圈信息
bool TWMJProtocolsManager::CreateBroadCurrentWindPacket(WorldPacket *packet, const uint32 &rountid, const TWMJTablePlayers *pTp)
{
	if (!packet)
		return false;
	if (pTp == NULL)
		return false;
	packet->clear();
	packet->SetOpcode(SMSS_MJ_CurrWind);
	*packet << uint16(SMSS_MJ_CurrWind) << uint16(5);
	*packet << uint8(rountid)
			<< uint8(pTp->pos);
        Log.Debug("TWMJProtocolsManager::CreateBroadCurrentWindPacket",
                  "rountid[%d] windpos[%d]", rountid,pTp->pos);

        packet->SetLength(packet->size());

	return true;
}
